<template>
  <div>
    <el-dialog
      :title="title"
      :visible.sync="dialogVisible"
      :close-on-click-modal="false"
      width="600px"
      @closed="resetForm('form')"
    >
      <div>
        <el-form ref="form" :rules="rules" :model="form" label-width="80px">
          <el-form-item
            :label="addType == 1 || addType == 3 ? '评分项' : '评分标准'"
            prop="content"
          >
            <el-input
              type="textarea"
              :rows="6"
              v-model.trim="form.content"
            ></el-input>
          </el-form-item>
          <el-form-item label="分值" v-if="addType == 2 || addType == 4">
            <el-input-number
              v-model="form.maxScore"
              :min="5"
              :max="100"
              size="medium"
              label="分值"
            ></el-input-number>
          </el-form-item>
          <el-form-item label="排序">
            <el-input-number
              v-model="form.orderBy"
              :min="1"
              :max="1000"
              size="medium"
              label="排序"
            ></el-input-number>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button type="custom" v-preventReClick @click="submitForm('form')"
          >提 交</el-button
        >
        <el-button @click="dialogVisible = false">取 消</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import {
  expertsScoreItemAdd,
  expertsScoreItemUpdate,
} from '@/api/admin/audit/expertsScoreItem.js'
import Bus from '@/utils/bus.js'
export default {
  data() {
    return {
      title: '新增',
      dialogVisible: false,
      addOrEdit: 1,
      checkedData: {},
      form: {
        pid: 0,
        content: '', //评分内容
        maxScore: '100', //最高分
        minScore: '0', //最低分
        orderBy: 10,
        scope: '', //适用范围
        type: '', //类型
        extFiled: '',
      },
      rules: {
        content: [
          { required: true, message: '请输入评分内容', trigger: 'blur' },
        ],
      },
    }
  },
  props: ['flowData', 'extFiled'],
  computed: {
    addType() {
      //1：新增一级 2：新增二级 3：编辑一级 4：编辑二级
      let type = 1
      if (this.checkedData.hasOwnProperty('pid')) {
        type = this.addOrEdit == 1 ? 2 : this.checkedData.pid == 0 ? 3 : 4
      }
      return type
    },
  },
  mounted() {
    Bus.$on('showDialogFlowAdd', (v) => {
      this.checkedData = {}
      if (v) {
        this.checkedData = v.row
        if (v.type == 'add') {
          this.handleAddOrEdit(1)
        } else {
          this.handleAddOrEdit(2)
        }
      } else {
        this.handleAddOrEdit(1)
      }
    })
  },
  beforeDestroy() {
    Bus.$off('showDialogFlowAdd')
  },
  methods: {
    //显示弹窗
    handleAddOrEdit(type) {
      if (type == 2) {
        this.title = '编辑'
        for (let i in this.checkedData) {
          if (this.form.hasOwnProperty(i)) {
            this.form[i] = this.checkedData[i] || ''
          }
        }
        this.form.id = this.checkedData.id
      } else {
        this.title = '新增'
        delete this.form.id
      }
      this.addOrEdit = type
      this.dialogVisible = true
    },
    submitForm(form) {
      if (this.flowData) {
        this.form.scope = this.flowData.scope
        this.form.type = this.flowData.extFiled
      } else if (this.extFiled) {
        this.form.scope = this.extFiled.deptId
        this.form.type = this.extFiled.courseType
        this.form.extFiled = this.extFiled.reviewId
      }
      if (this.addType == 1) {
        this.form.pid = 0
      } else if (this.addType == 2) {
        this.form.pid = this.checkedData.id
      }
      this.$refs[form].validate((valid) => {
        if (valid) {
          this.addOrEdit == 1 ? this.add() : this.edit()
        }
      })
    },
    //新增
    async add() {
      let res = await expertsScoreItemAdd(this.form)
      this.done(res)
    },
    //编辑
    async edit() {
      let res = await expertsScoreItemUpdate(this.form)
      this.done(res)
    },
    done(res) {
      if (!res || res.data.code != 0) return
      this.dialogVisible = false
      Bus.$emit('refreshFlowList')
      this.$message({
        message: '操作成功',
        type: 'success',
      })
    },
    //重置表单
    resetForm(form) {
      this.$refs[form].resetFields()
      this.form.content = ''
      this.form.maxScore = '100'
      this.form.minScore = '0'
      this.form.orderBy = 10
      this.form.type = ''
      this.form.scope = ''
      this.form.pid = '0'
      this.form.extFiled = ''
    },
  },
}
</script>